layui.use(['jquery', 'element', 'layer', 'form', 'layedit'], function(){
    var $ = layui.$;
    var layedit = layui.layedit;
    var form = layui.form;
    var ctxPath = "/education";

    /**
     * 详情富文本
     */
    var detailEditId = layedit.build('detail', {
        height: 500,
        uploadImage :  {
            url: ctxPath + '/file/uploadPic',
            type: 'post',
        }
    });

    /**
     * 初始化留学产品详情方法
     * @param id
     */
    var initStudyAbroadProduct = function(id) {
        $.ajax({
            url: ctxPath + '/api/studyAbroad/detail',
            type: 'post',
            dataType: 'json',
            data: {studyToursProductId: id},
            success: function (result) {
                if(result.code == 0) {
                    var data = result.data;

                    if (data != null) {
                        $("input[name='id']").val(data.id);
                        $("input[name='name']").val(data.name);
                        $("input[name='code']").val(data.code);
                        $("#previewImg").attr("src", data.showImage);
                        $("input[name='showImage']").val(data.showImage);
                        $("input[name='generation']").val(data.generation);
                        $("input[name='country']").val(data.country);
                        $("input[name='price']").val(data.price);
                        $("input[name='deposit']").val(data.deposit);

                        layedit.setContent(detailEditId, data.detail);

                        //渲染select
                        form.render('select');
                    }
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });
    }

    /**
     * 初始化留学产品
     * @type {string}
     */
    initStudyAbroadProduct(window.location.search.substring(4));

    /**
     * 新增游学产品
     */
    $("#saveBtn").click(function () {
        var name = $("[name='name']").val();
        var showImage = $("[name='showImage']").val();
        var code = $("[name='code']").val();
        var generation = $("[name='generation']").val();
        var country = $("[name='country']").val();
        var deposit = $("[name='deposit']").val();
        var price = $("[name='price']").val();
        var detail = $("[name='detail']").val();

        var request = {};

        request.name = name;
        request.showImage = showImage;
        request.code = code;
        request.generation = generation;
        request.country = country;
        request.deposit = deposit;
        request.price = price;
        request.detail = detail;

        $.ajax({
            url: ctxPath + "/api/studyAbroad/sync",
            data: JSON.stringify(request),
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if(result.code == 0) {
                    layer.alert('保存成功！', function(index){
                        layer.close(index);
                        location.href = ctxPath + '/html/studyAbroad/studyAbroadList.html';
                    });
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
    });


});

